//
//  CustomShareView.m
//  CustomShareStyle
//
//  Created by ljw on 16/6/2.
//  Copyright © 2016年 ljw. All rights reserved.
//

#import "CustomShareView.h"
#import "WXApi.h"
#import "TencentOAuth.h"
#import "WeiboSDK.h"
#define UIBounds [[UIScreen mainScreen] bounds] //window外框大小



@implementation CustomShareView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.hidden = YES;
        _huiseControl=[[UIControl alloc]initWithFrame:CGRectMake(0, 0, UIBounds.size.width, UIBounds.size.height)];
        
        [_huiseControl addTarget:self action:@selector(huiseControlClick) forControlEvents:UIControlEventTouchUpInside];
        _huiseControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        
        self.backgroundColor = [UIColor whiteColor];
        
        
        _shareListView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UIBounds.size.width, KScreenHeight/2-50)];
       
        
        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, UIBounds.size.width, 54)];
        title.font = FontP(17);
        title.textColor = [UIColor blackColor];
        title.textAlignment = NSTextAlignmentCenter;
        title.text = @"邀请好友";
        [_shareListView addSubview:title];
   
        UILabel *linLab = [[UILabel alloc] initWithFrame:CGRectMake(0, title.bottom, KScreenWidth, 1)];
        linLab.backgroundColor = RGB(243, 243, 243);
        [_shareListView addSubview:linLab];
        
        [self addSubview:_shareListView];
        
        
        _titleArray = [NSMutableArray array];
//        @[@"朋友圈",@"微信",@"QQ空间",@"微博",@"QQ",@"复制链接"]
        NSMutableArray *imageArray = [NSMutableArray array];
        if ([WXApi isWXAppInstalled]) {
            [_titleArray addObject:@"微信"];
            [imageArray addObject:@"p_center_share_button_0"];
            [_titleArray addObject:@"朋友圈"];
            [imageArray addObject:@"p_center_share_button_1"];

        }
        if ([TencentOAuth iphoneQZoneInstalled]) {
            [_titleArray addObject:@"QQ空间"];
            [imageArray addObject:@"p_center_share_button_2"];
            
        }
//        if ([WeiboSDK isWeiboAppInstalled]) {
//            [titleArray addObject:@"微博"];
//            [imageArray addObject:@"p_center_share_button_3"];
//        }
        if ([TencentOAuth iphoneQQInstalled]) {
            [_titleArray addObject:@"QQ"];
            [imageArray addObject:@"p_center_share_button_4"];
        }
        [_titleArray addObject:@"复制链接"];
        [imageArray addObject:@"p_center_share_button_5"];
     
        CGFloat btnW = (KScreenWidth-165)/4;
        CGFloat btnK = 33;
        CGFloat btnHK =50;

        CGFloat titleFont ;
        if (iPhone5) {
            titleFont = 8;
        }else if (iPhone6){
            titleFont = 9;
        }else{
            titleFont = 10;
        }

        for (int i = 0; i < _titleArray.count; i++) {
            UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(btnK+((i%4)*(btnW+btnK)), linLab.bottom+((i/4)*(btnHK+btnHK)), btnW, btnW);
            btn.tag = i;
            [btn setImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
            
            UILabel *titleLab =[[UILabel alloc]initWithFrame: CGRectMake(btnK+((i%4)*(btnW+btnK)), btn.bottom, btnW, 10)];
            titleLab.text = _titleArray[i];
            titleLab.font = FontP(titleFont);
            titleLab.textAlignment = NSTextAlignmentCenter;
            [btn addTarget:self action:@selector(shareControl:buttonTitle:) forControlEvents:UIControlEventTouchUpInside];

            [_shareListView addSubview:titleLab];
            [_shareListView addSubview:btn];
        }
    }
    return self;
}


- (void)showInView:(UIView *) view {
    if (self.isHidden) {
        self.hidden = NO;
        if (_huiseControl.superview==nil) {
            [view addSubview:_huiseControl];
        }
        [UIView animateWithDuration:0.2 animations:^{
            _huiseControl.alpha=1;
        }];
        CATransition *animation = [CATransition  animation];

        animation.duration = 0.2f;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.type = kCATransitionPush;
        animation.subtype = kCATransitionFromTop;
        [self.layer addAnimation:animation forKey:@"animation1"];
        self.frame = CGRectMake(0,view.frame.size.height - KScreenWidth/2-50, UIBounds.size.width, KScreenHeight/2-50);
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(12, 12)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
        [view addSubview:self];
    }
}


- (void)hideInView {
    if (!self.isHidden) {
        self.hidden = YES;
        CATransition *animation = [CATransition  animation];

        animation.duration = 0.2f;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.type = kCATransitionPush;
        animation.subtype = kCATransitionFromBottom;
        [self.layer addAnimation:animation forKey:@"animtion2"];
        [UIView animateWithDuration:0.2 animations:^{
            _huiseControl.alpha=0;
        }completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }
}

- (void)shareControl:(UIButton *)sender buttonTitle:(NSString *)title{
    if ([self.delegate respondsToSelector:@selector(shareControl:buttonTitle:)]) {
        [self.delegate shareControl:sender buttonTitle:_titleArray[sender.tag]];
    }

}

-(void)huiseControlClick{
    [self hideInView];
}


@end

